import tkinter
import cv2
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QFileDialog
import numpy as np
from skimage import io
from tkinter.filedialog import askopenfilename

def color_cus():
    top = tkinter.Toplevel()
    top.title('选择颜色')
    tkinter.Label(top, text='开始颜色 ', font=('fangsongti', 15)).grid(column=1, row=1)
    tkinter.Label(top, text='结束颜色 ', font=('fangsongti', 15)).grid(column=1, row=2)
    tkinter.Button(top, text='浏览', command=get_imagine_good, font=('fangsongti', 12), width=8).grid(column=2, row=1)
    tkinter.Button(top, text='浏览', command=get_imagine_bad, font=('fangsongti', 12), width=8).grid(column=2, row=2)

def get_imagine_good():  # 这个函数用来通过菜单栏中的Open来获取文件路径的功能
    def getpos(event, x, y, flags, param):
        if event == cv2.EVENT_LBUTTONDOWN:  # 定义一个鼠标左键按下去的事件
            print('Nice!')
            fo = open('./data/custom/color_custom_good.txt', "w")
            fo.write(str(HSV[y, x][0]))
            fo.write(',')
            fo.write(str(HSV[y, x][1]))
            fo.write(',')
            fo.write(str(HSV[y, x][2]))

    cap = cv2.VideoCapture(0)
    while(1):
        ret, frame = cap.read()
        k = cv2.waitKey(1)
        if k == 27:
            break
        cv2.imshow("Press ESC to exit", frame)
        cv2.setMouseCallback("Press ESC to exit", getpos)
        HSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    cap.release()
    cv2.destroyAllWindows()
    cv2.waitKey(0)

def get_imagine_bad():  # 这个函数用来通过菜单栏中的Open来获取文件路径的功能
    def getpos(event, x, y, flags, param):
        if event == cv2.EVENT_LBUTTONDOWN:  # 定义一个鼠标左键按下去的事件
            print('Nice!')
            fo = open('./data/custom/color_custom_bad.txt', "w")
            fo.write(str(HSV[y, x][0]))
            fo.write(',')
            fo.write(str(HSV[y, x][1]))
            fo.write(',')
            fo.write(str(HSV[y, x][2]))

    cap = cv2.VideoCapture(0)
    while (1):
        ret, frame = cap.read()
        k = cv2.waitKey(1)
        if k == 27:
            break
        cv2.imshow("Press ESC to exit", frame)
        cv2.setMouseCallback("Press ESC to exit", getpos)
        HSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    cap.release()
    cv2.destroyAllWindows()
    cv2.waitKey(0)
